package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {
    @Override
    public PageResponseResult findByQuery(SensitiveDTO dto) {
        dto.checkParam();
        Page<AdSensitive> pageReq = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdSensitive> queryWrapper = Wrappers.lambdaQuery();
        // 敏感词
        if (StringUtils.isNotBlank(dto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        IPage<AdSensitive> pageResult = this.page(pageReq,queryWrapper);
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPage(), dto.getSize(), pageResult.getTotal(), pageResult.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        if (StringUtils.isBlank(adSensitive.getSensitives())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词不能为空");
        }
        if (adSensitive.getSensitives().length() > 10) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词长度不能大于10个字符");
        }
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"敏感词不可以重复");
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        if(adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词id不能为空");
        }
        AdSensitive oldSensitive = getById(adSensitive.getId());
        if (oldSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不存在");
        }
        if (StringUtils.isNotBlank(adSensitive.getSensitives()) && !adSensitive.getSensitives().equals(oldSensitive.getSensitives())) {
            if (adSensitive.getSensitives().length() > 10) {
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词长度不能大于10个字符");
            }
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery()
                    .eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
            if(count > 0) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "该敏感词已存在");
            }
        }
        updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult delete(String id) {
        AdSensitive oldSensitive = getById(id);
        if (oldSensitive == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"敏感词不存在");
        }
        removeById(id);
        return ResponseResult.okResult();
    }

    @Autowired
    private SensitiveMapper  sensitiveMapper;

    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        return ResponseResult.okResult(sensitiveMapper.findAllSensitives());
    }
}
